1 module hip.gui.grid_layout;
2 public import hip.gui.widget;
3 
4 class GridLayout : Widget
5 {
6     enum Direction
7     {
8         horizontal, vertical
9     }
10 
11     protected int spacingX, spacingY, maxChildrenInCurrentDirection;
12     protected Direction plotDirection;
13 
14     this(int maxChildren, Direction d = Direction.horizontal)
15     {
16         setPlottingDirection(d);
17         setMaxChildrenInCurrentDirection(maxChildren);
18     }
19     
20 
21     void setPlottingDirection(Direction d)
22     {
23         plotDirection = d;
24         updateLayout();
25     }
26 
27     void setMaxChildrenInCurrentDirection(int maxChildren)
28     {
29         maxChildrenInCurrentDirection = maxChildren;
30         updateLayout();
31     }
32     void setSpacing(int spx, int spy)
33     {
34         spacingX = spx;
35         spacingY = spy;
36         updateLayout();
37     }
38     final void setSpacing(int spx){setSpacing(spx, spx);}
39     
40 
41     protected void updateLayout()
42     {
43         int childX, childY;
44 
45         foreach(i, ch; children)
46         {
47             childX = cast(int)(i % maxChildrenInCurrentDirection) * spacingX;
48             childY = cast(int)(i / maxChildrenInCurrentDirection) * spacingY;
49             if(plotDirection == Direction.vertical)
50             {
51                 int temp = childX;
52                 childX = childY;
53                 childY = temp;
54             }
55             ch.setPosition(childX, childY);
56         }
57     }
58     alias addChild = Widget.addChild;
59     override void addChild(Widget w)
60     {
61         super.addChild(w);
62         updateLayout();
63     }
64     override void onRender()
65     {
66         foreach(ch; children) if(ch.visible)
67             ch.render();
68         import hip.api;
69         Bounds b = getWorldBounds;
70         drawRectangle(b.x, b.y, b.width, b.height, HipColor.green);
71     }
72 }